package wiz.client.ui.viewimpl;

import wiz.client.WizStrings;
import wiz.client.activities.GameScoreView;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class GameScoreViewImpl extends Composite implements GameScoreView {

	private static GameScoreViewImplUiBinder uiBinder = GWT.create(GameScoreViewImplUiBinder.class);
	
	private final WizStrings fWizStrings = GWT.create(WizStrings.class);

	interface GameScoreViewImplUiBinder extends UiBinder<Widget, GameScoreViewImpl> {
	}

	public GameScoreViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@UiField
	FlexTable fGameSummaryTable;

	@Override
	public HasText[] createAndAddSummaryTableHeader(int numPlayers) {
		HasText toReturn[] = new HasText[numPlayers];
		int rowCount = fGameSummaryTable.getRowCount();
		fGameSummaryTable.insertRow(rowCount);
		
		fGameSummaryTable.addCell(rowCount);
		fGameSummaryTable.setWidget(rowCount, 0, new Label(fWizStrings.round()));
		fGameSummaryTable.getCellFormatter().setStyleName(rowCount, 0, "header");
		
		for(int i = 0; i < numPlayers; i++){
			fGameSummaryTable.addCell(rowCount);
			Label l = new Label();
			fGameSummaryTable.setWidget(rowCount, i + 1,l );
			fGameSummaryTable.getCellFormatter().setStyleName(rowCount, i+1, "header");
			toReturn[i] = l;
		}
		return toReturn;
	}

	@Override
	public SummaryTableCell[] createAndAddSummaryTableRow(int numPlayers) {
		int rowCount = fGameSummaryTable.getRowCount();
		fGameSummaryTable.insertRow(rowCount);
		fGameSummaryTable.addCell(rowCount);
		fGameSummaryTable.setWidget(rowCount, 0, new Label(fWizStrings.roundX(rowCount)));
		
		if(rowCount % 2 == 0){
			fGameSummaryTable.getCellFormatter().setStyleName(rowCount, 0, "cellTableOddRow");
		}
		
		
		SummaryTableCell toReturn[] = new SummaryTableCell[numPlayers];
		for(int i = 0; i < numPlayers; i++){
			PlayerRoundScoreCellViewImpl scoreCell = new PlayerRoundScoreCellViewImpl();
			fGameSummaryTable.setWidget(rowCount, i+1, scoreCell);
			toReturn[i] = new SummaryTableCell(new SpanHasText(scoreCell.fBidSpan), new SpanHasText(scoreCell.fMadeSpan), new SpanHasText(scoreCell.fScoreSpan));
			
			if(rowCount % 2 == 0){
				fGameSummaryTable.getCellFormatter().setStyleName(rowCount, i+1, "cellTableOddRow");
			}
		}
		
		return toReturn;
		
	}

	@Override
	public void clearTable() {
		fGameSummaryTable.removeAllRows();
	}

}
